#!/bin/bash

set -e

DATA_DIR=/tmp/example_mysql
mkdir -p "$DATA_DIR"

podman run --rm -d --name example_mysql -v $DATA_DIR:/var/lib/mysql \
        -e MYSQL_ROOT_PASSWORD=root123456 \
        -e MYSQL_USER=username \
        -e MYSQL_PASSWORD=password \
        -e MYSQL_DATABASE=database \
        -p 3306:3306 mysql:8.0
while ! podman exec example_mysql mysql -uusername -ppassword database -e 'select 1;' >/dev/null; do
    echo waitting for mysql to start up
    sleep 2
done
podman exec -i example_mysql mysql -uusername -ppassword database <example.sql

test ! -f ./go.mod && go mod init example

export MYBG_CONFIG=$(pwd)/example.yaml

mkdir -p ./model
pushd ./model
mybg model -w -f -F t_no_pk t_string_pk t_ai_pk
popd

mkdir -p ./dao
pushd ./dao
mybg dao -w -f t_no_pk t_string_pk t_ai_pk
#gopls imports -w t_no_pk.go
#gopls imports -w t_string_pk.go
#gopls imports -w t_ai_pk.go
gofmt -w t_no_pk.go t_string_pk.go t_ai_pk.go
popd

go mod tidy -v

podman stop example_mysql
